import { createStore } from 'vuex'
import { getCartsList } from 'network/cart.js'

export default createStore({
  state: {
    loading: false,
    Token: '',
    Token_type: '',
    cartsCount: 0,
  },
  mutations: {
    showLoading(state) {
      state.loading = true;
    },
    hideLoading(state) {
      state.loading = false;
    },
    setToken(state, token) {
      state.Token = token;
    },
    setToken_type(state, type) {
      state.Token_type = type;
    },
    setCartsCount(state, count) {
      state.cartsCount = count;
    }
  },
  actions: {
    updateCartsCount({ commit }) {
      getCartsList().then(res => {
        console.log(res)
        commit('setCartsCount', res.data.length || 0)
      })
    },
  },
  getters: {
    getIsLogin(state) {
      return state.Token ? true : false;
    },
    getCartsCount(state) {
      return state.cartsCount;
    },
  },
  modules: {
  }
})
